package com.offcn.coder;

public class Test02 {
    public static void main(String[] args) throws InterruptedException {
        MyThread t = new MyThread();
        t.start();
        //t.setStopped();
        //通过设置线程的标志位结束线程
        t.setStopped();
        t.join();
    }
}
//通过设置线程结束的标志位结束线程
class MyThread extends Thread {
    private boolean stopped = false;
    public void run() {
        while (!stopped) {
            System.out.println("running");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public void setStopped() {
        this.stopped = true;
    }
}